<template>
  <el-dialog
    v-model="dialogVisible"
    title="用户注册"
    width="450px"
    :before-close="handleClose"
    class="auth-dialog"
  >
    <el-form
      ref="registerFormRef"
      :model="registerForm"
      :rules="registerRules"
      label-width="100px"
      label-position="left"
    >
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="registerForm.username"
          placeholder="请输入用户名"
          clearable
          prefix-icon="User"
        />
      </el-form-item>

      <el-form-item label="邮箱" prop="email">
        <el-input
          v-model="registerForm.email"
          placeholder="请输入邮箱地址"
          clearable
          prefix-icon="Message"
        />
      </el-form-item>

      <el-form-item label="密码" prop="password">
        <el-input
          v-model="registerForm.password"
          type="password"
          placeholder="请输入密码"
          clearable
          prefix-icon="Lock"
          show-password
        />
      </el-form-item>

      <el-form-item label="确认密码" prop="confirmPassword">
        <el-input
          v-model="registerForm.confirmPassword"
          type="password"
          placeholder="请再次输入密码"
          clearable
          prefix-icon="Lock"
          show-password
        />
      </el-form-item>

      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="registerForm.realName"
          placeholder="请输入真实姓名"
          clearable
        />
      </el-form-item>

      <el-form-item label="手机号码" prop="phone">
        <el-input
          v-model="registerForm.phone"
          placeholder="请输入手机号码"
          clearable
          prefix-icon="Phone"
        />
      </el-form-item>

      <el-form-item>
        <el-checkbox v-model="registerForm.agreeTerms">
          我已阅读并同意
          <el-link type="primary" @click="showTerms">《用户协议》</el-link>
          和
          <el-link type="primary" @click="showPrivacy">《隐私政策》</el-link>
        </el-checkbox>
      </el-form-item>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button
          type="primary"
          :loading="registerLoading"
          @click="handleRegister"
        >
          注册
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
/**
 * 注册对话框组件
 * 
 * 功能：
 * - 用户注册表单
 * - 表单验证
 * - 注册状态管理
 * - 后端接口对接
 */

import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

console.log('RegisterDialog组件加载')

const props = defineProps({
  modelValue: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:modelValue', 'register-success'])

// 响应式数据
const registerFormRef = ref()
const registerLoading = ref(false)

const registerForm = ref({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  realName: '',
  phone: '',
  agreeTerms: false
})

// 计算属性
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

// 简化登录逻辑，移除复杂验证

// 表单验证规则
// 简化的注册验证规则
const registerRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  email: [
    { required: true, message: '请输入邮箱地址', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ],
  confirmPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' }
  ],
  realName: [
    { required: true, message: '请输入真实姓名', trigger: 'blur' }
  ]
}

// 方法定义
const handleClose = () => {
  console.log('关闭注册对话框')
  dialogVisible.value = false
  resetForm()
}

const resetForm = () => {
  registerFormRef.value?.resetFields()
  registerForm.value = {
    username: '',
    email: '',
    password: '',
    confirmPassword: '',
    realName: '',
    phone: '',
    agreeTerms: false
  }
}

const handleRegister = async () => {
  try {
    await registerFormRef.value.validate()
    
    console.log('提交注册表单:', registerForm.value.username)
    registerLoading.value = true
    
    // 简化注册逻辑：任意信息都能注册成功
    await new Promise(resolve => setTimeout(resolve, 800)) // 模拟网络延迟
    
    // 创建模拟用户信息
    const mockUser = {
      id: Date.now(),
      username: registerForm.value.username,
      email: registerForm.value.email,
      realName: registerForm.value.realName,
      phone: registerForm.value.phone,
      role: '普通用户',
      avatar: '',
      registerTime: new Date().toISOString()
    }
    
    console.log('注册成功，用户信息:', mockUser)
    ElMessage.success('注册成功，请登录')
    emit('register-success', mockUser)
    handleClose()
  } catch (error) {
    console.error('表单验证失败:', error)
    ElMessage.error('请检查输入信息')
  } finally {
    registerLoading.value = false
  }
}

const showTerms = () => {
  console.log('显示用户协议')
  ElMessage.info('用户协议页面开发中')
}

const showPrivacy = () => {
  console.log('显示隐私政策')
  ElMessage.info('隐私政策页面开发中')
}
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* 继承父组件的对话框样式 */
:deep(.el-dialog) {
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}

:deep(.el-dialog__header) {
  background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
  color: white;
  padding: 20px 24px;
  border-radius: 12px 12px 0 0;
}

:deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
  font-size: 18px;
}

:deep(.el-dialog__body) {
  padding: 24px;
  background: #fafbfc;
}

:deep(.el-form-item__label) {
  color: #374151;
  font-weight: 600;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  border: 1px solid #d1d5db;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  border: none;
  box-shadow: 0 2px 4px rgba(59, 130, 246, 0.3);
}

:deep(.el-checkbox__label) {
  color: #374151;
}
</style>
